Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused parameters, use CachingWriter, simplify a bit #79

Merged
merged 8 commits into from
Nov 27, 2024

Conversation

gnodet
Copy link
Contributor

@gnodet gnodet commented Nov 26, 2024

Alternative proposal to #77 as I really don't see the point in reimplementing the Properties.store() method.

// to write the file using a Writer.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
properties.store(baos, null);
String nl = System.lineSeparator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need system independent output. Use \n unconditionally.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
properties.store(baos, null);
String nl = System.lineSeparator();
String output = baos.toString(StandardCharsets.ISO_8859_1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're leaning pretty heavily on Hyrum's Law here. I'm not convinced we can rely on the description of current encoding in the Javadoc to apply to future encoding. In particular, I would not be at all surprised to see properties.store write in UTF-8 in a future JDK.

Copy link
Contributor Author

@gnodet gnodet Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been this way since JDK 1.2, so 26 years. And for the encoding, the Properties class has been opened to other encodings in JDK 1.5. The default encoding for reading properties has already been changed in JDK 9. The encoding used here is kinda irrelevant (whether ISO-8859-1 or UTF-8) because all non-ascii chars have been transformed into \uxxxx sequences, thereby removing everything that is encoding specific.

@gnodet gnodet requested a review from elharo November 27, 2024 09:24
properties.store(baos, null);
// The encoding can be either UTF-8 or ISO-8859-1, as any non ascii character
// is transformed into a \\uxxxx sequence anyway
String output = baos.toString(StandardCharsets.UTF_8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably 8859-1 to avoid the risk of someone slipping in a byte order mark.

.filter(line -> !line.startsWith("#"))
.sorted()
.collect(Collectors.joining("\n", "", "\n")); // system independent new line
try (Writer pw = new CachingWriter(outputFile, StandardCharsets.UTF_8)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pw --> writer

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class PomPropertiesUtilTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an additional test in the latest draft of my PR you can adopt.

@gnodet gnodet merged commit 7230d75 into apache:master Nov 27, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants